package com.shengqin.controller.api;

import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.baomidou.lock.executor.RedisTemplateLockExecutor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.common.pojo.CommonResult;
import com.shengqin.config.CommonException;
import com.shengqin.entity.*;
import com.shengqin.service.IFreeTestApplyService;
import com.shengqin.service.IPlanReservationItemService;
import com.shengqin.service.IReservationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/reservation")
@Slf4j
public class ReservationController {

    @Resource
    private IReservationService reservationService;
    @Resource
    private IPlanReservationItemService planReservationItemService;
    @Autowired
    private LockTemplate lockTemplate;
    @Resource
    private IFreeTestApplyService freeTestApplyService;

    @PostMapping("/queryPage")
    public CommonResult<Page<Reservation>> queryPage(@RequestBody CommonPage<Reservation> reservationPage){
        return CommonResult.data(reservationService.queryPage(reservationPage));
    }

    @PostMapping("/queryFreeTestApplyPage")
    public CommonResult<Page<FreeTestApply>> queryFreeTestApplyPage(@RequestBody CommonPage<FreeTestApply> reservationPage){
        return CommonResult.data(freeTestApplyService.queryPage(reservationPage));
    }

    @PostMapping("/login")
    public CommonResult<Student> login(@RequestBody Reservation reservation){
        return CommonResult.data(reservationService.login(reservation));
    }

    @PostMapping("/updatePassword")
    public CommonResult<Boolean> updatePassword(@RequestBody Student student){
        return CommonResult.data(reservationService.updatePassword(student));
    }

    @PostMapping("/applyFreeTest")
    public CommonResult<Boolean> applyFreeTest(@RequestBody FreeTestApply freeTestApply){
        return CommonResult.data(reservationService.applyFreeTest(freeTestApply));
    }

    @PostMapping("/reviewFreeTest")
    public CommonResult<Boolean> reviewFreeTest(@RequestBody FreeTestApply freeTestApply){
        return CommonResult.data(reservationService.reviewFreeTest(freeTestApply));
    }

    @PostMapping("/apply")
    public CommonResult<Boolean> apply(@RequestBody Reservation reservation){
        final LockInfo lockInfo = lockTemplate.lock(reservation.getStudyCode(), 3000, 10000L, RedisTemplateLockExecutor.class);
        if (null == lockInfo) {
            throw new RuntimeException("服务器繁忙，请稍后再试");
        }
        try{
            return CommonResult.data(reservationService.apply(reservation));
        } catch (Exception e){
            log.error("申请预约报错",e);
            throw new CommonException(e.getMessage());
        } finally {
            lockTemplate.releaseLock(lockInfo);
        }
    }

    @PostMapping("/cancelApply")
    public CommonResult<Boolean> cancelApply(@RequestBody Reservation reservation){
        return CommonResult.data(reservationService.cancelApply(reservation));
    }


    @PostMapping("/queryPlanList")
    public CommonResult<List<PlanReservationItem>> queryPlanList(@RequestBody Reservation reservation){
        return CommonResult.data(reservationService.queryPlanList(reservation));
    }

    @PostMapping("/queryByItemId")
    public CommonResult<PlanReservationItem> queryByItemId(@RequestBody PlanReservationItem planReservationItem){
        return CommonResult.data(planReservationItemService.getById(planReservationItem.getId()));
    }

    @PostMapping("/queryMyReservationList")
    public CommonResult<List<Reservation>> queryMyReservationList(@RequestBody Reservation reservation){
        return CommonResult.data(reservationService.queryMyReservationList(reservation));
    }
}
